---
category: Misc
created: '2020-04-19'
title: Convert rgb color to hex
updated: '2021-10-13'
---

**JavaScript version**

```js rgbToHex.js
const rgbToHex = (red, green, blue) => `#${((1 << 24) + (red << 16) + (green << 8) + blue).toString(16).slice(1)}`;

// Or
const rgbToHex = (red, green, blue) => `#${[red, green, blue].map((v) => v.toString(16).padStart(2, '0')).join('')}`;
```

**TypeScript version**

```ts rgbToHex.ts
const rgbToHex = (red: number, green: number, blue: number): string =>
    `#${((1 << 24) + (red << 16) + (green << 8) + blue).toString(16).slice(1)}`;

// Or
const rgbToHex = (red: number, green: number, blue: number): string =>
    `#${[red, green, blue].map((v) => v.toString(16).padStart(2, '0')).join('')}`;
```

**Examples**

```js examples.js
rgbToHex(0, 255, 255); // '#00ffff'
```

## See also

-   [Convert hex to rgb](https://phuoc.ng/collection/1-loc/convert-hex-to-rgb/)
-   [Create an RGB to HEX color converter](https://phuoc.ng/collection/react-drag-drop/create-an-rgb-to-hex-color-converter/)
